package com.kotlin.base.utils

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.widget.PopupWindow
import android.widget.RelativeLayout
import android.widget.TextView

import com.kotlin.base.R

class PopupToast(private var mActivity: Activity?, message: String, imageRes: Int, textColor: Int, duration: Int) : IToast {
    private var mPopupWindow: PopupWindow? = null

    init {
        val mPopupWindow: PopupWindow? = PopupWindow(mActivity)
        //        mPopupWindow.setAnimationStyle(R.style.dialogAnim);
        mPopupWindow?.isFocusable = true// 返回键pop消失,否则直接退出Activity
        mPopupWindow?.isOutsideTouchable = true// 点击pop外其他区域,pop消失
        mPopupWindow?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))// 背景透明,否则存在边框
        val view = createView(mActivity!!, message, imageRes, textColor)
        mPopupWindow?.contentView = view
        /* 设置大小,否则不显示 */
        mPopupWindow?.width = RelativeLayout.LayoutParams.MATCH_PARENT
        mPopupWindow?.height = RelativeLayout.LayoutParams.WRAP_CONTENT
    }

    private fun createView(context: Context, message: String, imageRes: Int, textColor: Int): View {
        val view = (context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater).inflate(R.layout.toast_layout, null, false)
        val textView = view.findViewById(R.id.toast_msg) as TextView
        if (imageRes > 0) {
            textView.setCompoundDrawablesWithIntrinsicBounds(imageRes, 0, 0, 0)
        }
        if (textColor > 0) {
            textView.setTextColor(context.resources.getColor(textColor))
        }
        textView.text = message
        return view
    }

    override fun show() {
        if (mPopupWindow != null && mActivity != null) {
            mPopupWindow!!.showAtLocation(mActivity!!.window.decorView,
                    Gravity.BOTTOM, 0, 0)
        }
    }

    fun show(window: Window?) {
        if (mPopupWindow != null && window != null && mActivity != null && mActivity!!.window != null && mActivity!!.window.decorView != null) {
            //
            val decorView = window.decorView
            val y = (decorView.y + decorView.height).toInt()
            val x = decorView.x.toInt()
            //
            mPopupWindow!!.width = mActivity!!.window.decorView.width
            //
            mPopupWindow!!.showAtLocation(decorView, Gravity.BOTTOM, -x, -y)
        }
    }

    override fun cancel() {
        if (mPopupWindow != null && mActivity != null) {
            try {
                mPopupWindow!!.dismiss()
            } catch (e: Throwable) {

            }

            mPopupWindow = null
            mActivity = null
        }
    }

    companion object {


        fun makeText(context: Activity, message: String, imageRes: Int, textColor: Int, duration: Int): PopupToast {
            return PopupToast(context, message, imageRes, textColor, duration)
        }
    }
}
